﻿using System;
using System.Windows;
using System.Windows.Input;
using ICSharpCode.AvalonEdit.Editing;

namespace EffectEd
{
    /// <summary>
    /// Interaction logic for SearchWindow.xaml
    /// </summary>
    public partial class SearchWindow : Window
    {
        public SearchWindow()
        {
            InitializeComponent();
        }

        public HlslEditor.HlslEdit Editor { get; set; }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var index = Editor.Text.ToLower().IndexOf(textBoxSearch.Text.ToLower(), Editor.TextArea.Caret.Offset, StringComparison.InvariantCulture);
            if (index >= 0)
            {
                Editor.TextArea.Caret.Offset = index + textBoxSearch.Text.Length;
                Editor.TextArea.Selection = new SimpleSelection(index, Editor.TextArea.Caret.Offset);
                Editor.TextArea.Caret.BringCaretToView();
            }
        }

        private void Window_Activated(object sender, EventArgs e)
        {
            textBoxSearch.Focus();
        }

        private void textBoxSearch_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                Button_Click(sender, new RoutedEventArgs());
            }
        }

    }
}
